package com.hrc.springbootdemo.ai;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@Slf4j
public class AiController {

    @Resource
    private ChatClientFactory chatClientFactory;


    /**
     * 阻塞式请求
     */
    @RequestMapping("/test")
    public String test(String prompt) {
           ChatClient chatClient = chatClientFactory.create(ChatClientEnum.QINIU);;
        return chatClient.prompt().user(prompt).call().content();
    }

    /**
     * 流式请求
     * 流式响应默认没有指定编码，所以需要指定编码，否则会出现乱码
     */

    @RequestMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String prompt) {
        ChatClient chatClient = chatClientFactory.create(ChatClientEnum.QINIU);;
        return chatClient.prompt().user(prompt).stream().content();
    }


}
